﻿using System;
using System.Data;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text;
using System.IO;
using System.Reflection;
using Castle.MonoRail.Framework.Helpers;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Script.Serialization;

namespace WebMVC
{
    /// <summary>
    /// 功能:Json辅助类
    /// [11-06-13 16:33  周一 YuQiang]<para />
    /// </summary>
    public class JsonHelper
    {
        /// <summary>
        /// 功能:判断json是否为空
        /// [11-07-07 09:48  周四 yuqiang]<para />
        /// </summary>
        /// <param name="strjson"></param>
        /// <returns></returns>
        public static bool CheckNullAndEmpty(string strjson)
        {
            if (!string.IsNullOrEmpty(strjson) && strjson != "[\r\n]")
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 功能:返回一个实体类的json字符串
        /// [11-06-13 16:34  周一 YuQiang]<para />
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ModelToJson(object obj)
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            new JsonSerializer().Serialize(new JsonTextWriter(sw), obj);
            return sb.ToString();
        }
        /// <summary>
        /// 功能:将datatable数据转换成JSON数据
        /// [11-06-13 16:34  周一 YuQiang]<para />
        /// </summary>
        /// <param name="jsonName"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DataTableToJson(string jsonName, DataTable dt)
        {
            StringBuilder Json = new StringBuilder();
            Json.Append("[");
            JavaScriptSerializer jss = new JavaScriptSerializer();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Json.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                      
                        Json.Append(dt.Columns[j].ColumnName.ToStr() + ":" + jss.Serialize(dt.Rows[i][j].ToStr()) + "");
                        if (j < dt.Columns.Count - 1)
                        {
                            Json.Append(",");
                        }
                    }
                    Json.Append("}");
                    if (i < dt.Rows.Count - 1)
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("]");
            return Json.ToString();
        }

        /// <summary>
        /// 格式化成JSON数据
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static string GeShiHua(object Value)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Serialize(Value.ToStr(""));
        }

        
        /// <summary>
        /// 功能:将datatable数据转换成JSON数据(专用于生产Grid Json 格式)  
        /// [11-06-13 16:34  周一 YuQiang]<para />
        /// </summary>
        /// <param name="PageNum"></param>
        /// <param name="PageCount"></param>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static string DataTableToJsonGrid(int PageCount, DataSet ds)
        {
            DataTable dt = ds.Tables[0];
            StringBuilder Json = new StringBuilder();
            string strnewLine = System.Environment.NewLine;
            Json.Append("{");
            Json.Append(strnewLine);
            Json.AppendFormat("\"total\":{0},", PageCount);  
            Json.Append(strnewLine);
            Json.Append("\"rows\": [");
            Json.Append(strnewLine);
            JavaScriptSerializer jss = new JavaScriptSerializer();
            if (dt.Rows.Count > 0)
            {
                //{"code":"001","name":"Name 1","addr":"Address 11","col4":"col4 data"}, 
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Json.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                      
                        if (dt.Rows[i][j].GetType() == typeof(string))
                        {
                            Json.Append("\"" + dt.Columns[j].ColumnName.ToStr() + "\"" + ":" + jss.Serialize(dt.Rows[i][j].ToStr("", false)) + "");
                        }
                        else
                        {
                            Json.Append("\"" + dt.Columns[j].ColumnName.ToStr() + "\"" + ":\"" + dt.Rows[i][j].ToStr("", false) + "\"");
                        }
                        if (j < dt.Columns.Count - 1)
                        {
                            Json.Append(",");
                        }
                    }
                    Json.Append("}");
                    if (i < dt.Rows.Count - 1)
                    {
                        Json.Append(",");
                    }
                    Json.Append(strnewLine);
                }
            }
            Json.AppendLine("]");
            Json.AppendLine("}");
            return Json.ToString();
        }

        
    }
}
